import { reactive, ref } from 'vue'
import { defineStore } from 'pinia'
import { axiosRequests } from '../hooks/axiosRequest'
export const userInfoStore = defineStore('userInfoStore', () => {
  const { userpost } = axiosRequests()
  interface userObj {
    area: string,
    birthday: string,
    createtime: string,
    email: string,
    hobby: string,
    logintime: string,
    nickname: string,
    sex: string,
    files: string,
  }
  const userInfo = ref<userObj>()//个人用户信息
  const photo = ref<string | undefined>(new URL('../../assets/photo/photo.png', import.meta.url).href)//头像路径
  const updateValue = (val: string) => {
    console.log("修改值", val);
  }
  //初始化用户基本信息
  function init(val: any) {
    console.log("我被调用了", val);
    userInfo.value = val
    photo.value = userInfo.value?.files
    console.log("www", userInfo.value);
  }
  //获取个人基本信息
  async function getInitInfo() {
    let result = await userpost('http://127.0.0.1:3100/myUserinfo/userinfo', {})
    console.log("asd", result);

    if (result.status === 0) {
      //对token重新获取
      localStorage.setItem('token', result.token)
      console.log("tt", result.data);
      init(result.data)
      return result
    } else {
      alert(result.data)
    }
  }

  return { userInfo, photo, init, updateValue, getInitInfo }
})
